Grokking Microservices Design Patterns
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
The Course at a Glance
Strangler Fig Pattern
Introduction
The Problem: Legacy Systems
The Strangler Pattern: A Solution
The Architecture of the Strangler Pattern
Strangler Pattern: A Detailed Example
Key Insights and Implications
API Gateway Pattern
Introduction to the API Gateway Pattern
Advantages of API Gateway Pattern
API Gateway Pattern: An Example
Performance Implications
System Design Example
Backends for Frontends (BFF) Pattern
Introduction to BFF
The Problem: Traditional Backend Models
The Architecture of the BFF Pattern
BFF Pattern: An Example
Performance Implications
System Design Examples
Service Discovery Pattern
What is Service Discovery Pattern?
The Problem: Service Coordination in Distributed Systems
Service Discovery Pattern: A Solution
The Architecture of the Service Discovery Pattern
The Inner Workings of the Service Discovery Pattern
Service Discovery Pattern: An Example
Performance Implications and Special Considerations
System Design Examples
Security Considerations
Circuit Breaker Pattern
Introduction
The Problem: The Struggles of Distributed Systems and Service Failures
The Circuit Breaker Pattern: An Effective Shield Against Cascading Failures
Circuit Breaker Pattern: An Example
Performance Implications and Special Considerations
System Design Examples
Summary
Bulkhead Pattern
Introduction
The Problem: Failure Propagation in Distributed Systems
The Bulkhead Pattern: A Solution
The Architecture
The Inner Workings
Bulkhead Pattern: A Example
Performance Implications and Special Considerations
System Design Examples
Conclusion
Retry Pattern
Introduction
The Retry Pattern: A Solution to Unreliable External Resources
The Architecture of the Retry Pattern
Retry Pattern: An Example
Performance Implications
Use Cases and System Design Examples
Conclusion
Sidecar Pattern
Introduction to the Sidecar Pattern
The Problem: Monolithic Application Management
A Solution to the Monolithic Mayhem
The Architecture of the Sidecar Pattern
Sidecar Pattern: Bringing Theory to Practice with an Example
Performance Implications
System Design Examples: Bringing the Sidecar Pattern to Life
Saga Pattern
Introduction to Saga Pattern
The Problem: Traditional Transaction Models
The Saga Pattern: A Solution
The Architecture of the Saga Pattern
The Inner Workings of the Saga Pattern
Saga Pattern: A Example
Performance Implications
System Design Examples
Conclusion
Introduction
The Problem: Managing Complex Interactions in Distributed Systems
Event-Driven Architecture: A Promising Solution
The Architecture of the Event-Driven Architecture Pattern
The Inner Workings of the Event-Driven Architecture Pattern
Event-Driven Architecture Pattern: An Example
Performance Implications and Special Considerations
Use Cases and System Design Examples
Conclusion
Introduction
The Problem: Traditional CRUD Operations
CQRS Pattern: A Solution
The Architecture of the CQRS Pattern
The Inner Workings of the CQRS Pattern
CQRS Pattern: An Example
Issues, Special Considerations, and Performance Implications
System Design Examples
Introduction
The Problem: Configuration Management in a Microservices Architecture
The Solution: Configuration Externalization Pattern
Unveiling the Architecture: How Does Configuration Externalization Work?
Delving into Code: An Example
Considerations and Implications
Use Cases and Real-world Examples
Conclusion
Embrace the Future of Software Architecture
The Architecture of the Saga Pattern

We've discussed what the Saga Pattern is and why it's essential in distributed systems. We've also seen how the pattern works in general. Now, let's get under the hood and understand the architecture that powers this ingenious pattern. But before we begin, let's recall that in the world of distributed systems, the Saga Pattern is our "power line" — it's the element that ensures transactions across different services are consistent and reliable. So, how does it do it?

The Backbone: Local Transactions

A saga is composed of multiple local transactions. Each of these transactions is executed within a single service and its corresponding database. These local transactions form the backbone of the Saga Pattern, akin to the individual sections of a power line. Each local transaction carries its own responsibilities but when strung together, they work towards a common objective — maintaining data consistency across the system.

Remember, every local transaction in a saga has an associated compensating transaction. The compensating transaction is capable of undoing the changes made by its corresponding local transaction. Together, these transactions ensure that even if a step in the saga fails, the system can still maintain its integrity.

Two Approaches: Choreography and Orchestration

As mentioned earlier, there are two types of Saga Patterns - Choreography and Orchestration. These two approaches represent the architectural choices you have when implementing the Saga Pattern. They differ in how they coordinate the execution of the local transactions in a saga.

In the Choreography Saga Pattern, each service knows which service to call next in the saga. It's a decentralized approach, with no single point of orchestration. Imagine an ant colony, where each ant knows its tasks and performs them without being explicitly told to do so. The colony functions smoothly as a result of each ant playing its part.

In contrast, the Orchestration Saga Pattern involves a central coordinator, or an orchestrator, which guides the saga's execution. Think of it as a busy airport with an air traffic controller. The controller guides each airplane (service) when to take off (start a transaction) and when to land (end a transaction) to ensure smooth operation.

So, should you be the ant or the air traffic controller? The answer largely depends on your use-case and the complexity of your business transaction. While choreography may offer more autonomy to your services, orchestration can provide you more control and easier error handling.

The Heart of the Matter: Eventual Consistency

At the heart of the Saga Pattern is the concept of eventual consistency. It's a consistency model that's more suited to distributed systems. Unlike ACID transactions, which promise immediate consistency, the Saga Pattern ensures that the system will eventually reach a consistent state, even if it goes through inconsistent states in the process.

This can be likened to juggling. At any given moment, some balls (or transactions) are in the air (being processed), but a skilled juggler (the Saga Pattern) ensures that eventually, all balls will be handled correctly without dropping.

Handling Failures: Compensating Transactions

The saga has a built-in mechanism to handle failures — the compensating transactions. If a local transaction fails, the saga executes compensating transactions for the previously successful local transactions. This brings the system back to a consistent state.

To picture this, imagine walking on a path and realizing you've gone the wrong way. What do you do? You retrace your steps to the point where you took the wrong turn. That's what compensating transactions do. They "undo" the changes to get the system back to the point before the error occurred.

Pit Stops: Saga Log

To keep track of the saga's progress and state, a saga log is maintained. This log records every local transaction

and compensating transaction executed in the saga. In our city analogy, think of the saga log as the city blueprint. It gives a detailed view of what has been done and what needs to be done next.

This brings us to the end of our exploration of the Saga Pattern's architecture. It's not a simple path, but then again, nothing worth doing ever is, right? Armed with this understanding, you're now ready to delve into the specifics of implementing the Saga Pattern in Java.

In our next section, we will walk you through a detailed Java code example illustrating the Saga Pattern in action. Ready to take the plunge? Let's dive in!

The Saga Pattern: A Solution
The Inner Workings of the Saga Pattern
Mark as Completed

On this page

The Backbone: Local Transactions

Two Approaches: Choreography and Orchestration

The Heart of the Matter: Eventual Consistency

Handling Failures: Compensating Transactions

Pit Stops: Saga Log